//
//  HomeViewCell.swift
//  XLWB
//
//  Created by 郭亮 on 16/3/17.
//  Copyright © 2016年 gl. All rights reserved.
//

import UIKit


class HomeViewCell: UITableViewCell {
    // MARK:-VM属性
    var statusVm: StatusViewModel? {
        didSet {
            guard let statusVm = statusVm else {
            return
            }
            // 设置头像
            iconImageView.sd_setImageWithURL(statusVm.iconUrl, placeholderImage: UIImage(named: "avatar_default"))
            
            // 设置认证图标
            verifiedImageview.image = statusVm.verifiedImage
            
            // 设置昵称
            nameLable.text = statusVm.status?.user?.screen_name
            
            // 设置VIP图标
            vipImageView.image = statusVm.vipImage
            
            // 设置时间的Label
            timeLable.text = statusVm.display_at
            
            // 设置来源
            sourceLable.text = statusVm.sourceText
            
            // 设置微博正文
            textLable.text = statusVm.status?.text
            
            // 转发微博文字
            retweedTextLable.text = statusVm.retweetedText
            
            // 传递urls给collectionV
            collectionV.pictureUrls = statusVm.pictureUrls
            
            // 设置转发微博内容
            if statusVm.status?.retweeted_status != nil {
                // 有转发微博
                retweedTextLable.text = "@" + (statusVm.status?.retweeted_status?.user?.screen_name ?? "") + ": " + (retweedTextLable.text ?? "")
                retweedTextLableBottomCons.constant = 15
                backgroudV.hidden = false
            }else {
                //没转发微博
                retweedTextLable.text = nil
                retweedTextLableBottomCons.constant = 0
                backgroudV.hidden = true
            }
            
            // 有无图片的情况
            toolBarTopCons.constant = statusVm.pictureUrls.count == 0 ? 0 : 15
        }
    }
    
    // MARK:-控件属性
    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var verifiedImageview: UIImageView!
    @IBOutlet weak var nameLable: UILabel!
    @IBOutlet weak var timeLable: UILabel!
    @IBOutlet weak var sourceLable: UILabel!
    @IBOutlet weak var vipImageView: UIImageView!
    @IBOutlet weak var textLable: UILabel!
    @IBOutlet weak var toolBar: UIView!
    @IBOutlet weak var collectionV: HomeCellCollectionView!
    @IBOutlet weak var retweedTextLable: UILabel!
    @IBOutlet weak var backgroudV: UIView!
    
    @IBOutlet weak var textLableWidthCons: NSLayoutConstraint!
    @IBOutlet weak var retweedTextLableBottomCons: NSLayoutConstraint!
    @IBOutlet weak var toolBarTopCons: NSLayoutConstraint!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        textLableWidthCons.constant = UIScreen.mainScreen().bounds.width - 2 * edgeMargin
    }
    
        

}

extension HomeViewCell {
    ///cell高度计算
    func cellHeight(statusVm: StatusViewModel) -> CGFloat {
        self.statusVm = statusVm
        layoutIfNeeded()
        return CGRectGetMaxY(toolBar.frame)
    }
}









